<?php
namespace  Ekadmin\Controller;

use \Think\Controller;

use Think\Page;



class CaiwuController extends CommonController

{

        public  function index()

        {




            $where['uniacid'] = $this->uniacid;

            $obj = M('caiwu');



            $param = I('get.');

            if($param['type']){
            
                $where['type'] = $param['type'];
            
            }
            
            
            if($param['uid']){
                $where['uid'] = $param['uid'];
            }
            

            if($param['start'] && !$param['end']){
            
                $where['addtime'] = array('gt',strtotime($param['start']));
            
            }
            
            if(!$param['start'] && $param['end']){
            
                $where['addtime'] = array('lt',strtotime($param['end']));
            
            }
            
            if($param['start'] && $param['end']){
            
                $where['addtime'] = array('between',[strtotime($param['start']),strtotime($param['end'])]);
            
            }
            
            $count = $obj->where($where)->count();

            $page = new Page($count);
            
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
            
            if($list){
                for($i=0;$i<count($list);$i++){
                    $rs=M("user")->find($list[$i]["uid"]);
                    $rt=M("xiaoqu")->where("uid=".$list[$i]["uid"]." and uniacid=".$this->uniacid)->find();
                    if($rt){
                        $list[$i]["name"]=$rt["tuan_name"];
                        $list[$i]["mobile"]=$rt["mobile"];
                    }else{
                        $list[$i]["name"]=$rs["name"];
                        $list[$i]["mobile"]=$rs["mobile"];
                    }
                    
                    //$list[$i]["mobile"]=$rs["mobile"];
                    $list[$i]["acv"]=$rs["head"];
                    switch($list[$i]["type"]){
                        case 1:
                            $list[$i]["type"]="<font color='green'>收入</font>";
                            break;
                        case 2:
                            $list[$i]["type"]='<font color="red">支出</font>';
                            break;
                    }
                }
            }

            $this->assign('page',$page->show());
            
            $this->assign('count',$count);

            $this->assign('list',$list);

            
            $this->display();

        }

        

        /**
         *@批量删除记录 
         */
        public function del()

        {

            if(IS_POST){

                $param = I('id');

                if(is_array($param)){

                    $map['id'] = array('in',$param);

                }else{

                    $map['id'] = $param;

                }
                
                $map["uniacid"]=$this->uniacid;


                M('caiwu')->where($map)->delete();

                ajax_return(0,'删除成功');

            }

        }
        
        /**
         *@导出财务明细 
         */
        public function exportorder(){
            
            $param = I('get.');
            
            $where["uniacid"]=$this->uniacid;
            
            if($param['uid']){
            
                $where['uid'] = $param['uid'];
            
            }
            
            if($param['type']){
            
                $where['type'] = $param['type'];
            
            }           
            
            if($param['start'] && !$param['end']){
            
                $where['addtime'] = array('gt',strtotime($param['start']));
            
            }
            
            if(!$param['start'] && $param['end']){
            
                $where['addtime'] = array('lt',strtotime($param['end']));
            
            }
            

            if($param['start'] && $param['end']){
            
                $where['addtime'] = array('between',[strtotime($param['start']),strtotime($param['end'])]);
            
            }           
            
            
            $xlsCell = array(
            
                ['id','编号'],
            
                ['uid','会员信息'],
                
                ['type','类型'],
                
                ['money', '金额'],
                
                ['addtime','变动时间'],
                
                ['info','变动信息'],
            
                ['balance',  '当前余额']
                
            );
            
            $data = M('caiwu')->where($where)->field('year,day,month',TRUE)->select();
            
            foreach ($data as $k=>$v){
                
                $rs=M("user")->where("id=".$v["uid"]." and uniacid=".$this->uniacid)->find();
                
                $data[$k]["uid"]="姓名：".$rs["name"].",手机号：".$rs["mobile"];
                
                $data[$k]["type"]=$v["type"]==1? "收入":"支出";
                
                $data[$k]["addtime"]=date("Y-m-d H:i:s",$data[$k]["addtime"]);
            }
            
            
            if($data){
            
                if($param['start'] && $param['end'] ){
                    $title = $param['start'].'--'.$param['end'].'期间财务明细';
                }else{
                    $title=date("YmdHis")."期间财务明细";
                }
            
                exportExcel($title,$xlsCell,$data);
            
            }else{
            
                echo "<script>alert('没有记录');</script>";
                exit();
            
            }
            
            
        }

}